import time
from Panda.Core.Log import logger


# from message import MessageDict
class MessageDict(dict):
    def __getattr__(self, key):
        """get dict[x] by dict.x"""
        keyname = key[0].upper() + key[1:]
        # keyname = key
        try:
            value = self[keyname]
        except KeyError:
            return
        return value

    def get(self, v, d=None):
        """get func refact"""
        try:
            return self[v]
        except KeyError:
            return d


class Article:
    def __init__(self, title, description, picurl, url):
        self.title = title
        self.description = description
        self.picurl = picurl
        self.url = url


class Response:

    def __init__(self, msg: MessageDict, **kwargs):
        self.kwargs = kwargs
        self.toUserName = msg.fromUserName
        self.fromUserName = msg.toUserName
        self.time = int(time.time())

    @property
    def to_xml(self):
        raise NotImplementedError


class TextResponse(Response):
    def __init__(self, msg: MessageDict, content=""):
        super().__init__(msg)
        self.content = content

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[{self.content}]]></Content>
                </xml>"""

    def __repr__(self):
        return f"TextResponse({self.content})"


class ImageResponse(Response):
    def __init__(self, msg: MessageDict, media, **kwargs):
        super().__init__(msg)
        self.media = media
        self.path = kwargs.get("path", "")

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[image]]></MsgType>
                    <Image>
                        <MediaId><![CDATA[{self.media}]]></MediaId>
                    </Image>
                </xml>"""

    def __repr__(self):
        return f"ImageResponse({self.path})"


class VoiceResponse(ImageResponse):
    def __init__(self, msg: MessageDict, media, **kwargs):
        super().__init__(msg, media, **kwargs)

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[voice]]></MsgType>
                    <Voice>
                        <MediaId><![CDATA[{self.media}]]></MediaId>
                    </Voice>
                </xml>"""


class VideoResponse(ImageResponse):
    def __init__(self, msg: MessageDict, media, title="", description=""):
        super().__init__(msg, media)
        self.title = title
        self.description = description

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[video]]></MsgType>
                    <Video>
                        <MediaId><![CDATA[{self.media}]]></MediaId>
                        <Title><![CDATA[{self.title}]]></Title>
                        <Description><![CDATA[{self.description}]]></Description>
                    </Video> 
                </xml>"""

    def __repr__(self):
        return f"videoResponse({self.title})"


class MusicResponse(VideoResponse):
    def __init__(
        self,
        msg: MessageDict,
        media,
        title="",
        description="",
        music_url="",
        hq_music_url="",
        pic_media="",
    ):
        super().__init__(msg, media, title, description)
        self.music_url = music_url
        self.hq_music_url = hq_music_url
        self.media = pic_media

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[music]]></MsgType>
                    <Music>
                        <Title><![CDATA[{self.title}]]></Title>
                        <Description><![CDATA[{self.description}]]></Description>
                        <MusicUrl><![CDATA[{self.music_url}]]></MusicUrl>
                        <HQMusicUrl><![CDATA[{self.hq_music_url}]]></HQMusicUrl>
                        <ThumbMediaId><![CDATA[{self.meida}]]></ThumbMediaId>
                    </Music>
                </xml>"""

    def __repr__(self):
        return f"MusicResponse({self.title}, {self.music_url})"


class NewsResponse(Response):
    def __init__(self, msg: MessageDict, article: Article):
        super().__init__(msg)
        self.article = article

    @property
    def to_xml(self):
        return f"""<xml>
                    <ToUserName><![CDATA[{self.toUserName}]]></ToUserName>
                    <FromUserName><![CDATA[{self.fromUserName}]]></FromUserName>
                    <CreateTime>{self.time}</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>1</ArticleCount>
                    <Articles>
                        <item>
                            <Title><![CDATA[{self.article.title}]]></Title>
                            <Description><![CDATA[{self.article.description}]]></Description>
                            <PicUrl><![CDATA[{self.article.picurl}]]></PicUrl>
                            <Url><![CDATA[{self.article.url}]]></Url>
                        </item>
                    </Articles>
                </xml>"""

    def __repr__(self):
        return f"NewsResponse({self.article.title})"


# def make_text_response(msg: MessageDict, content=""):
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[text]]></MsgType>
#                     <Content><![CDATA[{content}]]></Content>
#                 </xml>"""


# def make_image_response(msg: MessageDict, meida):
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[image]]></MsgType>
#                     <Image>
#                         <MediaId><![CDATA[{meida}]]></MediaId>
#                     </Image>
#                 </xml>"""


# def make_voice_response(msg: MessageDict, meida):
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[voice]]></MsgType>
#                     <Voice>
#                         <MediaId><![CDATA[{meida}]]></MediaId>
#                     </Voice>
#                 </xml>"""


# def make_video_response(msg: MessageDict, meida, title="", description=""):
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[video]]></MsgType>
#                     <Video>
#                         <MediaId><![CDATA[{meida}]]></MediaId>
#                         <Title><![CDATA[{title}]]></Title>
#                         <Description><![CDATA[{description}]]></Description>
#                     </Video>
#                 </xml>"""


# def make_music_response(
#     msg: MessageDict, meida, title="", description="", music_url="", hq_music_url=""
# ):
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[music]]></MsgType>
#                     <Music>
#                         <Title><![CDATA[{title}]]></Title>
#                         <Description><![CDATA[{description}]]></Description>
#                         <MusicUrl><![CDATA[{music_url}]]></MusicUrl>
#                         <HQMusicUrl><![CDATA[{hq_music_url}]]></HQMusicUrl>
#                         <ThumbMediaId><![CDATA[{meida}]]></ThumbMediaId>
#                     </Music>
#                 </xml>"""


# def make_news_response(msg: MessageDict, articles: list[Article]):
#     items = ""
#     for article in articles:
#         item = f"""<item>
#             <Title><![CDATA[{article.title}]]></Title>
#             <Description><![CDATA[{article.description}]]></Description>
#             <PicUrl><![CDATA[{article.picurl}]]></PicUrl>
#             <Url><![CDATA[{article.url}]]></Url>
#         </item>"""
#         items += item
#     return f"""<xml>
#                     <ToUserName><![CDATA[{msg.fromUserName}]]></ToUserName>
#                     <FromUserName><![CDATA[{msg.toUserName}]]></FromUserName>
#                     <CreateTime>{int(time.time())}</CreateTime>
#                     <MsgType><![CDATA[news]]></MsgType>
#                     <ArticleCount>{len(articles)}</ArticleCount>
#                     <Articles>
#                         {items}
#                     </Articles>
#                 </xml>"""
